//
// Created by Administrator on 2025/3/20.
//


#include "ui_elf_not_connect_net.h"
static void ui_elf_not_connect_net_screen_event(lv_event_t *e){
    LOG_INFO_WRAPPER("start");
//    lv_group_send_data()
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    lv_group_t *group = lv_group_get_default();
    int  event_data = (int)lv_event_get_user_data(e);
    if (event_code == LV_EVENT_KEY)
    {
        // 按键事件 获取按键值
        uint32_t lv_key = lv_event_get_key(e);
        switch (lv_key)
        {
            case LV_KEY_UP:
                LV_LOG_USER("=========== touched KeyUp");
//                ui_word_query_keyboard_left_key(group,target,event_data);
                break;
            case LV_KEY_DOWN:
                LV_LOG_USER("============ touched KeyDown");
//                ui_word_query_right_keyboard_key(group, target, event_data);
                break;
            case LV_KEY_LEFT:
                LV_LOG_USER("=========== touched KeyLeft");
                // 不等于3，不等于提示框
                if(event_data != 3){
                    lv_group_focus_prev(group);
                }
//                lv_group_focus_obj(event_data->prev_obj);
//                ui_word_query_keyboard_left_key(group,target,event_data);
//                lv_group_focus_prev(group);

                break;
            case LV_KEY_RIGHT:
                LV_LOG_USER("============ touched KeyRight");
                // 不等于3，不等于提示框
                if(event_data != 3){
                    lv_group_focus_next(group);
                }
//                lv_group_focus_obj(event_data->next_obj);
//                ui_word_query_right_keyboard_key(group, target, event_data);
//                lv_group_focus_next(group);

                break;
            case LV_KEY_ENTER:
                LV_LOG_USER("============ touched KeyEnter");

//                ui_elf_game_elf_info_enter_key(group,target,event_data);
                // 等于3，等于提示框
                if(event_data == 3){
                    lv_group_focus_prev(group);
                    lv_group_focus_prev(group);
                    lv_obj_add_flag(target, LV_OBJ_FLAG_HIDDEN);
                }else if(event_data == 1){
                    // 等于1，等于闯词app扫码连接
                    elf_game_module.screen->deinit_cb();
                    ui_elf_game_elf_info_screen_show(&elf_game_module);
                }else if(event_data == 0)
                {
                    // 等于0，等于返回页面
                    LOG_INFO_WRAPPER("= touched KeyEnter 0");
                }

                break;
            case LV_KEY_BACKSPACE:
                LV_LOG_USER("============ touched KeyBackspace");

                break;
            case LV_KEY_DEL:
                // 模拟删除字母
                LV_LOG_USER("============ touched keydel");
//                word_query_keyboard_del_key(group, target, event_data);
                break;
            case LV_KEY_SPACE:
                // 模拟输入字母
                LV_LOG_USER("============ touched space");
                break;
            case LV_KEY_ESC:
                LV_LOG_USER("============ touched ESC");
                break;
            default:
                break;
        }
    }
}

void ui_elf_not_connect_screen_init(ui_elf_game_main_screen_t *screen){
    lv_coord_t screen_width = lv_disp_get_hor_res(NULL);
    lv_coord_t screen_height = lv_disp_get_ver_res(NULL);
    if(screen->screen == NULL) {
        if (screen->group == NULL) {
            screen->group = ui_set_indev_group(indev_keypad);
        }
        screen->screen = ui_create_default_screen(lv_scr_act());

        // 屏幕panel
        lv_obj_t *main_panel = ui_create_column_default_panel(screen->screen);

        // top panel
        lv_obj_t *top_panel = ui_word_query_top_panel_component(main_panel);
        lv_obj_t *top_return_panel = lv_obj_get_child(top_panel, 1);
        lv_obj_set_style_border_side(top_return_panel, LV_BORDER_SIDE_BOTTOM,LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(top_return_panel, 2, LV_STATE_FOCUSED);
        lv_coord_t top_panel_height = lv_obj_get_style_height(top_panel, 0);
        lv_group_add_obj(screen->group, top_return_panel);
        lv_obj_add_event_cb(top_return_panel, ui_elf_not_connect_net_screen_event, LV_EVENT_KEY, 0);
        LOG_INFO_WRAPPER("top_panel_height: %d", top_panel_height);

        // middle panel row_flex
        lv_coord_t middle_panel_height = screen_height - top_panel_height;
        lv_obj_t *middle_panel = ui_create_column_panel(main_panel, screen_width,
                                                     middle_panel_height);
        lv_obj_set_flex_align(middle_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_left(middle_panel, 23, 0);
        lv_coord_t middle_panel_row = 14;
        lv_obj_set_style_pad_row(middle_panel, middle_panel_row, 0);
        lv_obj_set_style_pad_top(middle_panel, 25, 0);

        // middle panel top 未连接 图片
        lv_obj_t *middle_top_panel = ui_create_row_default_panel(middle_panel);
        lv_obj_set_style_pad_column(middle_top_panel, 8, 0);
        lv_obj_t *middle_top_img1 = ui_create_default_image_by_src(middle_top_panel, &elf_not_connect_net_label_img);
        lv_obj_t *middle_top_img2 = ui_create_default_image_by_src(middle_top_panel, &elf_not_connect_net_icon_img);

        // middle middle label
        lv_coord_t middle_middle_top_pad = 24;
//        lv_coord_t middle_middle_top_pad = 0;
        lv_obj_t *middle_middle_panel = ui_create_without_flex_panel(middle_panel, LV_SIZE_CONTENT, 20+middle_middle_top_pad-middle_panel_row);
        lv_obj_set_style_pad_top(middle_middle_panel, middle_middle_top_pad-middle_panel_row, 0);
        lv_obj_t *middle_middle_label = ui_create_default_label(middle_middle_panel, "当前连接网络:",CHINESE_FONT_16_LIGHT);
//        lv_obj_set_align(middle_middle_label, LV_ALIGN_BOTTOM_LEFT);
        // middle bottom panel
        lv_obj_t *middle_bottom_panel = ui_create_column_default_panel(middle_panel);
        lv_obj_set_style_pad_row(middle_bottom_panel, 11, 0);
        char *connect_methods[] = {"闯词APP扫码连接","本机直接连接"};
        for (int i = 0; i < sizeof (connect_methods)/sizeof(connect_methods[0]);++i) {
            // panel width 169 height 30
            lv_obj_t *middle_bottom_item_panel = ui_prompt_dialog(middle_bottom_panel, connect_methods[i], 169, 30,
                                                                  LV_SIZE_CONTENT,CHINESE_FONT_16_LIGHT);
            lv_obj_set_style_radius(middle_bottom_item_panel, 6, 0);
            lv_obj_set_style_pad_bottom(middle_bottom_item_panel, 4, 0);
            lv_obj_set_style_border_width(middle_bottom_item_panel, 2, LV_STATE_FOCUSED);
            lv_group_add_obj(screen->group, middle_bottom_item_panel);
            lv_obj_add_event_cb(middle_bottom_item_panel, ui_elf_not_connect_net_screen_event, LV_EVENT_KEY, (void *)i+1);

//            if( i == 0){
//                lv_group_focus_obj(middle_bottom_item_panel);
//            }
//            lv_obj_t *middle_bottom_item_panel = ui_create_without_flex_panel(middle_bottom_panel, 169, 30);
//            lv_obj_t *middle_bottom_item_label = ui_create_default_label(middle_bottom_item_panel, connect_methods[i], CHINESE_FONT_16_LIGHT);
        }

        // 需要联网提示框
        lv_obj_t *connect_net_prop_panel = ui_prompt_dialog(screen->screen, "请连接网络\n"
                                                                            "再来陪精灵玩哦~", 318, 158, LV_SIZE_CONTENT,
                                                            CHINESE_FONT_16_LIGHT);
        lv_obj_t *prop_label = lv_obj_get_child(connect_net_prop_panel, 0);
        // line space 4
        lv_obj_set_style_text_line_space(prop_label, 4, 0);
//        lv_obj_add_flag(connect_net_prop_panel, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_event_cb(connect_net_prop_panel, ui_elf_not_connect_net_screen_event, LV_EVENT_KEY, (void *)3);
        lv_group_add_obj(screen->group, connect_net_prop_panel);
        lv_group_focus_obj(connect_net_prop_panel);



    }
}

void ui_elf_not_connect_net_screen_show(elf_game_module_t *module){
    LOG_INFO_WRAPPER("start");
    if(module){
        if(module->screen->screen == NULL){
            ui_elf_not_connect_screen_init(module->screen);
        }
    }
    module->screen->current_page = ELF_GAME_ELF_NOT_CONNECT_NET_SCREEN;
    module->screen->deinit_cb = ui_elf_not_connect_net_screen_deinit;

}
void ui_elf_not_connect_net_screen_deinit(){
    LOG_INFO_WRAPPER("start");
    ui_screen_group_deinit_common(&(elf_game_module.screen->screen), &(elf_game_module.screen->group));
}
